/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package EventProcessor;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author luc0
 */
public class KeyPresserThread implements Runnable{

    private Robot robot;
    private int keyToPress;
    private int timeBetweenPresses;
    public KeyPresserThread(int keyToPress,int timeBetweenPresses){
        try {
            this.keyToPress = keyToPress;
            this.timeBetweenPresses = timeBetweenPresses;
            robot = new Robot();
        //    robot.setAutoDelay(timeBetweenPresses);
        } catch (AWTException ex) {
            Logger.getLogger(KeyPresserThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void run() {
        while(true){
            try {
                if(keyToPress==-1){
                    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    Thread.sleep(timeBetweenPresses);
                     robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                } else if(keyToPress==-2){
                    robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
                    Thread.sleep(timeBetweenPresses);
                    robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
                }else{
                robot.keyPress(keyToPress);
                Thread.sleep(timeBetweenPresses);
                robot.keyRelease(keyToPress);
                }
                
            } catch (InterruptedException ex) {
               Logger.getLogger(KeyPresserThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
    }

}
